using System;
using Castle.MonoRail.Framework;
using Saffron.Framework.Users;

namespace Saffron.Controllers
{
    [Layout("default"), Rescue("generalerror")]
    public class LoginController : PublicBaseController
    {
        public void Index()
        {
        }

        public void LoginRequest(string username, string password)
        {
            try
            {

                //Check if account is ok
                UserAccount account = UserServices.ValidateLogin(username, password);

                Session["user"] = account;

                //Redirect to User home page
                Redirect("useraccount", "index");
            }
            catch (System.Security.SecurityException)
            {

                PropertyBag["message"] = "The system could not log you on. The user name and password combination did not match our records.";
                RenderView("Index");
            }

        }

        public void Signup()
        {
        }

        public void CreateAccount([DataBind("useraccount")] UserAccount newAccount)
        {
            //Validate that user is ok
            newAccount.Create();

            //Login user
            Session["user"] = newAccount;
            RedirectToAction("signupconfirmation");
        }

        public void Signout()
        {
            Session["user"] = null;
            Redirect("home", "index");
        }

        public void SignupConfirmation()
        {
            
        }
    }
}
